package com.wei.frame;

import android.app.Application;
import android.content.Context;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.wei.frame.di.component.AppComponent;
import com.wei.frame.di.component.DaggerAppComponent;
import com.wei.frame.ui.imageloader.ImageLoader;

import java.text.SimpleDateFormat;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasAndroidInjector;

/**
 * 作者:赵若位
 * 时间:2020/8/20 2:16
 * 功能:
 */
public class App extends Application implements HasAndroidInjector {

    private static AppComponent sComponent;

    @Inject
    volatile DispatchingAndroidInjector<Object> mAndroidInjector;
    @Inject
    Context sContext;
    @Inject
    ImageLoader mImageLoader;


    static {
        //使用static代码段可以防止内存泄漏
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context).setDrawableSize(20));
        //全局设置默认的 Header
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            //开始设置全局的基本参数（这里设置的属性只跟下面的MaterialHeader绑定，其他Header不会生效，能覆盖DefaultRefreshInitializer的属性和Xml设置的属性）
//            layout.setEnableHeaderTranslationContent(false);
            layout.setPrimaryColorsId(R.color.clr_999999, R.color.clr_333333);//全局设置主题颜色
            return new ClassicsHeader(context).setTimeFormat(new SimpleDateFormat());
        });
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Logger.addLogAdapter(new AndroidLogAdapter());
        injectIfNecessary();
    }


    public static AppComponent getComponent() {
        return sComponent;
    }

    @Override
    public AndroidInjector<Object> androidInjector() {
        injectIfNecessary();
        return mAndroidInjector;
    }

    private void injectIfNecessary() {
        if (null == mAndroidInjector) {
            synchronized (this) {
                if (null == mAndroidInjector) {
                    AndroidInjector<App> applicationInjector = applicationInjector();
                    applicationInjector.inject(this);
                    if (null == mAndroidInjector) {
                        throw new IllegalStateException(
                                "The AndroidInjector returned from applicationInjector() did not inject the "
                                        + getPackageName() + ".App");
                    }
                }
            }
        }
    }

    private AndroidInjector<App> applicationInjector() {
        sComponent = DaggerAppComponent.builder().application(this).build();
        return sComponent;
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (null != mImageLoader) {
            mImageLoader.onTrimMemory(this, level);
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (null != mImageLoader) {
            mImageLoader.onLowMemory(this);
        }
    }
}
